$(document).ready(function(e) {
	$('#'+IParameterName.USERID).focus();
	if(popupBlockerDetected=="true"){
		var popupWindow = window.open("about:blank","","directories=no,height=100,width=100,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,top=0,location=no");
		
		detectPopup(popupWindow);
	}
});
function detectPopup(popupWindow) {
	var popupInfo = $("#popupBlockedInfo");
	var isBlocked = popupWindow==null || !popupWindow || typeof popupWindow == 'undefined';
	if(isBlocked) {
		popupInfo.removeAttr('style');
	} else {
		popupWindow.close();
	} 
}

$(document).on('click','#logInButton',  function(event) {
	login();
});

$(document).on('click','#logInButtonOneP',  function(event) {
	// S1M-6020 - check portal vs journal and add selected journal in url
	S1MUtils.cleanUpOldMessages("notificationDiv", IParameterName.WEB_VALIDATION_DIV );
	var onepURL = $('#logInButtonOneP').attr('href');     		
    
    if(childrenExists == 'true') {
    	if($('#'+IParameterName.LOGIN_CONFIG_ID).val() == '') {
    		// missing journal 
    		var loginNotifications = $('#notificationDiv');
        	var html='';
        	// top level validation
        	html+='<div id="loginError" class="alert alert-error">';
        	html+='<p><i class="fa fa-warning"></i> ' +  resources[IResourceName.LOGIN_PLEASE_SELECT_JOURNAL]+'</p></div>';
        	loginNotifications.append(html);
        	event.preventDefault();
    	} else {
    		if( $('#'+IParameterName.LOGIN_CONFIG_ID).val() == undefined) {
    			$('#logInButtonOneP').attr('href', onepURL + getSiteURL() + '/portal');
    		} else {
    			$('#logInButtonOneP').attr('href', onepURL + $('#'+IParameterName.LOGIN_CONFIG_ID).find(':selected').attr('data-orgpath'));
    		}    		
    	}
	} else {
		$('#logInButtonOneP').attr('href', onepURL + getSiteURL());
	}
});

function login(){
	S1MUtils.cleanUpOldMessages("notificationDiv", IParameterName.WEB_VALIDATION_DIV );
	if(isOnePInSession=='true'){
		$('#ONEP').val('Y');
    }
	var password =  $('#'+IParameterName.PASSWORD).val();
	var userID = $('#'+IParameterName.USERID).val();
	if(password=='' || userID==''){
		incorrectUser();
	} 
	if(childrenExists=='true' &&  $('#'+IParameterName.LOGIN_CONFIG_ID).val()==''){
		//missing journal 
		var loginNotifications = $('#notificationDiv');
    	var html='';
    	//top level validation
    	html+='<div id="loginError" class="alert alert-error">';
    	html+='<p><i class="fa fa-warning"></i> ' +  resources[IResourceName.LOGIN_PLEASE_SELECT_JOURNAL]+'</p></div>';
    	loginNotifications.append(html);
	}
	else{
		if(password!='' && userID!='')
		{
            if(enableOrcidSignON)
            {
	          setField('USER_S1M_LOGIN', 'Y');
            }
		    if(isRevisedPage)
			{
		      setField('IS_ORCID_TOKEN_IN_SESSION', 'Y');
			}
		    
			setDataAndNextPage(IParameterName.TAG_ACTION, loginAction, homePage);
		}
	}	
}

function incorrectUser(){
	$('#'+IParameterName.LOGIN_BAD_USERNAME_OR_PASSWORD).val('TRUE');
	if(isOnePInSession=='true'){
		$('#ONEP').val('Y');
    }
	setNextPage(loginPage);
}
$( document ).on( "keydown", function(event) {
   var keycode = (event.keyCode ? event.keyCode : event.which);
    if(keycode == '13'){
    	// SF 28649 - Delay for Chrome password auto-populate issue 
    	setTimeout(function(){login()}, 200);
    }
  });